﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;


namespace MvcEnrollment.Helpers
{
    public class RebillGatewayConnector
    {
        RebillResponse _response;

    //Change gateway in webconfig
        string _uri = ConfigurationManager.AppSettings["ewayGateway_Rebill"];

    int _timeout = 36000;

    public RebillGatewayConnector()
    {

    }

    /// <summary>
    /// The Uri of the Eway payment gateway
    /// </summary>
    public string Uri
    {
        get { return _uri; }
        set { _uri = value; }
    }

    public int ConnectionTimeout
    {
        get { return _timeout; }
        set { _timeout = value; }
    }

    public RebillResponse Response
    {
        get { return _response; }
    }

    /// <summary>
    /// Do the post to the gateway and retrieve the response
    /// </summary>
    /// <param name="GatewayRequest"></param>
    /// <returns></returns>
    public bool ProcessRequest(RebillPayment paymentRequest)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_uri);
        request.Method = "POST";
        request.Timeout = _timeout;
        request.ContentType = "application/x-www-form-urlencoded";
        request.KeepAlive = false;

        byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(paymentRequest.ToXML());
        request.ContentLength = requestBytes.Length;

        // Send the data out over the wire
        try
        {
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();
        }

        catch
        {
            // Net connection failed
            return false;
        }

        HttpWebResponse response = null;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException wex)
        {
            // for timeouts etc
            if (response == null)
                return false;

            // try and get the error text
            response = (HttpWebResponse)wex.Response;
            StreamReader sr = null;
            sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
            string _wexBody = sr.ReadToEnd();
            sr.Close();
            return false;
        }

        catch
        {
            return false;
        }

        // get the response
        if (response.StatusCode == HttpStatusCode.OK)
        {
            StreamReader sr = null;
            sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
            string _serverXml = sr.ReadToEnd();
            sr.Close();

            try
            {
                _response = new RebillResponse(_serverXml);
                return true;
            }

            catch
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    }
}